package xyz.quanguanjia.masterboy;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

public class PersonalInfoAdapter extends RecyclerView.Adapter {

    public static final int TYPE_PERSONAL_INFO_AVATAR = 1;

    public static final int TYPE_PERSONAL_INFO = 2;

    private List<PersonalInfoCellItemModel> models;

    public PersonalInfoAdapter(List<PersonalInfoCellItemModel> models) {
        this.models = models;
    }

    @Override
    public int getItemViewType(int position) {
        return models.get(position).type;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 创建列表项视图

        View itemView;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());

        switch (viewType) {
            case PersonalInfoAdapter.TYPE_PERSONAL_INFO_AVATAR:
                itemView = inflater.inflate(R.layout.layout_cell_personal_info_avatar, parent, false);
                return new PersonalInfoAdapter.MBDPersonalInfoViewHolder(itemView);
            case PersonalInfoAdapter.TYPE_PERSONAL_INFO:
                itemView = inflater.inflate(R.layout.layout_cell_personal_info, parent, false);
                return new PersonalInfoAdapter.MBDPersonalInfoViewHolder(itemView);
        }

        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        // 绑定数据到列表项视图
        PersonalInfoCellItemModel detail = this.models.get(position);
        ((PersonalInfoAdapter.MBDPersonalInfoViewHolder) holder).bind(detail);
    }

    @Override
    public int getItemCount() {
        return this.models.size();
    }

    public static class MBDPersonalInfoViewHolder extends RecyclerView.ViewHolder {
        private TextView titleTextView;
        private TextView infoDetailTextView;
        private ImageView detailImageView;


        public MBDPersonalInfoViewHolder(@NonNull View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.text_info_title);
            infoDetailTextView = itemView.findViewById(R.id.text_info_detail);
            detailImageView = itemView.findViewById(R.id.imageview_personal_info_avatar_detail);
        }

        public void bind(PersonalInfoCellItemModel detail) {
            // 设置列表项视图的数据
            titleTextView.setText(detail.title);
            if (null != infoDetailTextView) {
                infoDetailTextView.setText(detail.detail);
            }
            if (null != detailImageView) {
                String imageUrl = MBDHostManager.IMAGE_HOST + detail.image;
                Glide.with(MBDApplication.getContext())
                        .load(imageUrl)
                        .apply(RequestOptions.circleCropTransform())
                        .into(detailImageView);
            }

        }
    }
}
